﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace Service.ZSJL.Com.Graphic
{
     public delegate void GetImgDelgate(Image[] allImg);

     public partial class CutImg : Form
     {
         public event GetImgDelgate GetImgEvent;

         public CutImg()
         {
             InitializeComponent();
         }

         private void panel2_Paint(object sender, PaintEventArgs e)
         {
             Panel pnl = (Panel)sender;

             Pen pen = new Pen(Color.Red, 8);
             e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, pnl.Width - 1, pnl.Height - 1));


         }

         private void button1_Click(object sender, EventArgs e)
         {
             btnMove.Visible = false;
             //btnSize.Visible = false;
             panel2.Visible = false;

             PictureBox pic = new PictureBox();
             pic.Dock = DockStyle.Top;
             pic.BorderStyle = BorderStyle.FixedSingle;
             pic.SizeMode = PictureBoxSizeMode.Zoom;
             pic.Height = 150;

             Bitmap bit = new Bitmap(panel2.Width, panel2.Height);
             Graphics g = Graphics.FromImage(bit);
             g.CopyFromScreen(this.PointToScreen(panel2.Location), new Point(0, 0), bit.Size);
             pic.Image = bit;

             Clipboard.SetImage(bit);

             g.Dispose();

             panel3.Controls.Add(pic);
             btnMove.Visible = true;
             //btnSize.Visible = true;
             panel2.Visible = true;
         }

         private void button3_Click(object sender, EventArgs e)
         {
             this.Close();
         }


         private void button2_Click(object sender, EventArgs e)
         {
             btnMove.Visible = false;
             //btnSize.Visible = false;
             panel2.Visible = false;

             if (panel3.Controls.Count > 0)
             {
                 Image[] img = new Image[panel3.Controls.Count];

                 int i = 0;
                 foreach (PictureBox item in panel3.Controls)
                 {
                     img[i++] = item.Image;
                 }

                 if (GetImgEvent != null)
                 {
                     GetImgEvent(img);
                 }
             }
             else
             {
                 Bitmap bit = new Bitmap(panel2.Width, panel2.Height);
                 Graphics g = Graphics.FromImage(bit);
                 g.CopyFromScreen(this.PointToScreen(panel2.Location), new Point(0, 0), bit.Size);

                 Clipboard.SetImage(bit);

                 g.Dispose();

                 if (GetImgEvent != null)
                 {
                     GetImgEvent(new Image[] { bit });
                 }
             }

             btnMove.Visible = true;
             //btnSize.Visible = true;
             panel2.Visible = true;

             this.Close();

         }

         bool dFlag = false;
         int x = -1;
         int y = -1;
         private void panel2_MouseDown(object sender, MouseEventArgs e)
         {
             this.Cursor = Cursors.SizeNWSE; ;
             dFlag = true;
             x = e.X;
             y = e.Y;
         }

         private void panel2_MouseMove(object sender, MouseEventArgs e)
         {
             if (dFlag)
             {
                 int ww = panel2.Width + (e.X - x);
                 int hh = panel2.Height + (e.Y - y);

                 if (ww >= (this.Width - panel3.Width))
                 {
                     panel2.Width = this.Width - panel3.Width;
                 }
                 else
                 {
                     panel2.Width = ww;
                 }

                 if (hh >= (this.Height - panel1.Height))
                 {
                     panel2.Height = this.Height - panel1.Height;
                 }
                 else
                 {
                     panel2.Height = hh;
                 }

                 //  panel2.Left = (this.Width - panel3.Width - panel2.Width - 2) / 2;

                 x = e.X;
                 y = e.Y;

                 this.Refresh();
             }
         }

         private void panel2_MouseUp(object sender, MouseEventArgs e)
         {
             dFlag = false;
             this.Cursor = Cursors.Default;
         }

         private void button3_Click_1(object sender, EventArgs e)
         {
             int ww = panel2.Width + 20;
             int hh = panel2.Height + 20;

             if (ww >= (this.Width - panel3.Width))
             {
                 panel2.Width = this.Width - panel3.Width;
             }
             else
             {
                 panel2.Width = ww;
             }

             if (hh >= (this.Height - panel1.Height))
             {
                 panel2.Height = this.Height - panel1.Height;
             }
             else
             {
                 panel2.Height = hh;
             }

             panel2.Left = (this.Width - panel3.Width - panel2.Width - 2) / 2;
             this.Refresh();
         }

         private void button5_Click(object sender, EventArgs e)
         {
             int ww = panel2.Width - 20;
             int hh = panel2.Height - 20;

             if (ww <= 100 || hh <= 100) return;

             if (ww >= (this.Width - panel3.Width))
             {
                 panel2.Width = this.Width - panel3.Width;
             }
             else
             {
                 panel2.Width = ww;
             }

             if (hh >= (this.Height - panel1.Height))
             {
                 panel2.Height = this.Height - panel1.Height;
             }
             else
             {
                 panel2.Height = hh;
             }

             panel2.Left = (this.Width - panel3.Width - panel2.Width - 2) / 2;
             this.Refresh();
         }

         bool moved = false;
         int mx = -1;
         int my = -1;

         private void btnMove_MouseDown(object sender, MouseEventArgs e)
         {
             moved = true;
             mx = e.X;
             my = e.Y;
         }

         private void btnMove_MouseMove(object sender, MouseEventArgs e)
         {
             if (moved)
             {
                 panel2.Left = panel2.Left + (e.X - mx);
                 panel2.Top = panel2.Top + (e.Y - my);
             }
         }

         private void btnMove_MouseUp(object sender, MouseEventArgs e)
         {
             moved = false;
             mx = -1;
             my = -1;
         }

         //bool sized = false;
         //int sx = -1;
         //int sy = -1;

         //private void btnSize_MouseDown(object sender, MouseEventArgs e)
         //{
         //    sized = true;
         //    sx = e.X;
         //    sy = e.Y;
         //}

         //private void btnSize_MouseUp(object sender, MouseEventArgs e)
         //{
         //    sized = false;
         //    sx = -1;
         //    sy = -1;
         //}

         //private void btnSize_MouseMove(object sender, MouseEventArgs e)
         //{
         //    if (sized)
         //    {
         //        int ww = panel2.Width + (e.X - sx);
         //        int hh = panel2.Height + (e.Y - sy);

         //        if (ww >= (this.Width - panel3.Width))
         //        {
         //            panel2.Width = this.Width - panel3.Width;
         //        }
         //        else
         //        {
         //            panel2.Width = ww;
         //        }

         //        if (hh >= (this.Height - panel1.Height))
         //        {
         //            panel2.Height = this.Height - panel1.Height;
         //        }
         //        else
         //        {
         //            panel2.Height = hh;
         //        }

         //        //panel2.Left = (this.Width - panel3.Width - panel2.Width - 2) / 2;

         //        btnSize.Left = btnSize.Left + (e.X - sx);
         //        btnSize.Top = btnSize.Top + (e.Y - sy);

         //        sx = e.X;
         //        sy = e.Y;

         //        this.Refresh();
         //    }
         //}

         private void CaptureImageTool_Load(object sender, EventArgs e)
         {
             panel2.Left = (this.Width - panel3.Width - panel2.Width - 2) / 2;
         }

         private void panel2_MouseHover(object sender, EventArgs e)
         {
             this.Cursor = Cursors.SizeNWSE;

         }

         private void panel2_MouseLeave(object sender, EventArgs e)
         {
             this.Cursor = Cursors.Default;
         }

         private void btnMove_MouseHover(object sender, EventArgs e)
         {
             this.Cursor = Cursors.SizeAll;
         }
     }
}
